package com.mooc.yohoshop.ui.category;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.view.menu.MenuAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.mooc.yohoshop.R;
import com.mooc.yohoshop.entity.BaseEntity;
import com.mooc.yohoshop.entity.CategoryEntity;
import com.mooc.yohoshop.entity.MenuEntity;
import com.mooc.yohoshop.ui.base.BaseLazyFragment;
import com.mooc.yohoshop.widget.HomeBrandDivider;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;

public class CategoryFragment extends BaseLazyFragment<CategoryRepo> implements OnItemClickListener {

    private RecyclerView menu_rv,category_rv;
    private CategoryMenuAdapter menuAdapter;
    private CategoryAdapter categoryAdapter;

    @Override
    protected void lazyInit() {
        request(repo.requestMenu());
    }

    @Override
    protected int layout() {
        return R.layout.fragment_category;
    }

    @Override
    protected void init() {
        menu_rv = f(R.id.menu_rv);
        category_rv = f(R.id.category_rv);
        LinearLayoutManager menuManager = new LinearLayoutManager(getContext());
        menuManager.setOrientation(LinearLayoutManager.VERTICAL);
        menu_rv.setLayoutManager(menuManager);
        menuAdapter = new CategoryMenuAdapter();
        menuAdapter.setOnItemClickListener(this);
        menu_rv.setAdapter(menuAdapter);
        GridLayoutManager manager = new GridLayoutManager(getContext(),3);
        manager.setOrientation(GridLayoutManager.VERTICAL);
        categoryAdapter = new CategoryAdapter();
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (categoryAdapter.getItem(position).getItemType() == CategoryEntity.TITLE)
                    return 3;
                return 1;
            }
        });
        category_rv.setLayoutManager(manager);
        category_rv.setAdapter(categoryAdapter);
    }

    @Override
    protected void result(BaseEntity baseEntity) {
        if (baseEntity instanceof MenuEntity){
            MenuEntity menuEntity = (MenuEntity) baseEntity;
            menuAdapter.setNewInstance(menuEntity.data);
        }else if (baseEntity instanceof CategoryEntity){
            CategoryEntity categoryEntity = (CategoryEntity) baseEntity;
            CategoryEntity.Values v1 = new CategoryEntity.Values();
            v1.type = CategoryEntity.TITLE;
            v1.category_name="衣服";
            categoryEntity.data.add(0,v1);
            CategoryEntity.Values v2 = new CategoryEntity.Values();
            v2.type = CategoryEntity.TITLE;
            v2.category_name="短裤";
            categoryEntity.data.add(4,v2);
            categoryAdapter.setNewInstance(categoryEntity.data);
        }
    }

    @Override
    public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
        long id = menuAdapter.getItem(position).menu_id;
        for (int i = 0;i < menuAdapter.getData().size();i ++)
            menuAdapter.getItem(i).sel = false;
        menuAdapter.getItem(position).sel = true;
        menuAdapter.notifyDataSetChanged();
        HashMap<String,Object>map = new HashMap<>();
        map.put("menuId",""+id);
        request(repo.requestCategory(map));
    }
}
